//
//  AlertContentView.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/9/5.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class AlertContentView: UIView {
    
    var effectView:EffectView!
    
    var backClosure:TouchBlock!
    
    var backPassWordValue:BackStringValue!
    
    var type = 0 // 0 密码确认框  1 注册成功弹窗
    
    var leftClosure:TouchBlock!

    lazy var alertView:MyCustomAlert = {
      let view = Bundle.main.loadNibNamed("MyCustomAlert", owner: nil, options: nil)?.first as! MyCustomAlert
        view.frame = CGRect.init(x: (UIScreen.ScreenWidth - 250)/2, y: (UIScreen.ScreenHeight - 150)/2, width: 250, height: 150)
        view.center = self.center
        self.addSubview(view)
        view.alpha = 0
        view.setCornerRadio(radio: 20, isMask: false)
        view.setShadow(color: "000000", offset: CGSize.init(width: 0, height: 0), radius: 20, opacity: 0.01)
        view.leftClosure = { [weak self] in
            if self?.leftClosure != nil {
                self?.leftClosure()
            }
            self?.hiden()
        }
        view.rightClosure = { [weak self] in
            if self?.backClosure != nil {
                self?.backClosure()
            }
            self?.hiden()
        }
        return view
    }()
    
    lazy var passWordAlertView:PasswordAlert = {
        let view = Bundle.main.loadNibNamed("PasswordAlert", owner: nil, options: nil)?.first as! PasswordAlert
        view.frame = CGRect.init(x: (UIScreen.ScreenWidth - 250)/2, y: (UIScreen.ScreenHeight - 150)/2, width: 250, height: 150)
        self.addSubview(view)
        view.setCornerRadio(radio: 20, isMask: false)
        view.setShadow(color: "000000", offset: CGSize.init(width: 0, height: 0), radius: 20, opacity: 0.1)
        view.close = { [weak self] in
            self?.hidenAlert()
        }
        view.backPassWord = { [weak self] (password) in
            self?.backPassWordValue(password)
            self?.hidenAlert()
        }
        return view
    }()
    
    lazy var loginSuccess:LoginSuccessAlert = {
        let view = Bundle.main.loadNibNamed("LoginSuccessAlert", owner: nil, options: nil)?.first as! LoginSuccessAlert
        view.frame = CGRect.init(x: (UIScreen.ScreenWidth - 250)/2, y: (UIScreen.ScreenHeight - 150)/2, width: 250, height: 150)
        self.addSubview(view)
        view.setCornerRadio(radio: 20, isMask: false)
        view.setShadow(color: "000000", offset: CGSize.init(width: 0, height: 0), radius: 20, opacity: 0.1)
        view.confirmClosure = { [weak self] in
            if self?.backClosure != nil {
                self?.backClosure()
            }
            self?.hidenAlert()
        }
        return view
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.effectView = EffectView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: UIScreen.ScreenHeight))
        self.effectView.alpha = 0
        self.addSubview(self.effectView)
        self.effectView.hidenClosure = { [weak self]  in
            self?.hiden()
        }
    }
    
    init(frame: CGRect,type:Int) {
        super.init(frame: frame)
        
        self.type = type
        self.effectView = EffectView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: UIScreen.ScreenHeight))
        self.effectView.alpha = 0
        self.addSubview(self.effectView)
        self.effectView.hidenClosure = { [weak self]  in
            self?.hidenAlert()
        }
        if type == 0 {
            self.passWordAlertView.alpha = 0
        }else{
            self.loginSuccess.alpha = 0
        }
    }
    
    func showRechargeAlert(title:String,content:String){
        self.loginSuccess.updateViews(title: title, content: content)
        self.alpha = 1
        UIView.animate(withDuration: 0.2) {
            self.effectView.alpha = 1
            self.loginSuccess.alpha = 1
        }
    }
    
    
    func showAlert(){
        self.alpha = 1
        if self.type == 0 {
            UIView.animate(withDuration: 0.2, animations: {
                self.effectView.alpha = 1
                self.passWordAlertView.alpha = 1
            }) { (_) in
                self.passWordAlertView.field.becomeFirstResponder()
            }
        }else{
            UIView.animate(withDuration: 0.2) {
                self.effectView.alpha = 1
                self.loginSuccess.alpha = 1
            }
        }
    }
    
    func show(withTitle title:String){
        self.alpha = 1
        self.alertView.titleLabel.text = title
        UIView.animate(withDuration: 0.2) {
            self.effectView.alpha = 1
            self.alertView.alpha = 1
        }
    }
    
    func hidenRechargeAlert(){
        UIView.animate(withDuration: 0.2, animations: {
            self.effectView.alpha = 0
            self.alpha = 0
            self.loginSuccess.alpha = 0
        })
    }
    
    func hidenAlert(){
        self.passWordAlertView.field.resignFirstResponder()
        UIView.animate(withDuration: 0.2, animations: {
            self.effectView.alpha = 0
            self.alpha = 0
            if self.type == 0 {
                self.passWordAlertView.alpha = 0
            }else{
                self.loginSuccess.alpha = 0
            }
        })
    }
    
    func hiden(){
        
        UIView.animate(withDuration: 0.2, animations: {
            self.effectView.alpha = 0
            self.alertView.alpha = 0
            self.alpha = 0
        })
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
